package com.tools.cleanmaster.manager;

import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.HandlerThread;

import com.tools.cleanmaster.CleanApplication;
import com.tools.cleanmaster.taskmain.TKPubApi;
import com.tools.cleanmaster.utils.MemoryUtils;

import java.io.Serializable;
import java.util.ArrayList;

/**
 * CreatedXXX on 17/9/20.
 * 对后台应用按照占用内存大小排序，应用后需要及时销毁(防止内存，应用不具有实时行)
 */

public class SortProcessManager {
    private static SortProcessManager sortProcessManager;
    private ArrayList<ProcessItem> processItems;
    private HandlerThread handlerThread;
    private Handler mHandler;

    public class ProcessItem implements Serializable {
        private static final long serialVersionUID = 7812261183770300022L;
        public String pkgName;
        public String label;
        public Drawable icon;
        public int percent;
    }

    private SortProcessManager() {
        handlerThread = new HandlerThread("sortprocess");
        handlerThread.start();
        mHandler = new Handler(handlerThread.getLooper());
    }

    public static SortProcessManager getSortProcessManager() {
        if (sortProcessManager == null) {
            synchronized (SortProcessManager.class) {
                if (sortProcessManager == null) {
                    sortProcessManager = new SortProcessManager();
                }
            }
        }
        return sortProcessManager;
    }

    public ArrayList<ProcessItem> getProcessItems() {
        return processItems == null ? getUnProcessItems() : processItems;
    }

    public void startSort() {
        synchronized (SortProcessManager.class) {
            mHandler.post(new Runnable() {
                @Override
                public void run() {
                    ArrayList<com.tools.cleanmaster.taskmain.ProcessItem> list =
                            TKPubApi.getRunningSortProcesses(CleanApplication.getInstance(), true);
                    int mTitleRam = MemoryUtils.getAvailableRam(CleanApplication.getInstance(), list);
                    if (mTitleRam == 0) {
                        mTitleRam = list.size() <= 1 ? 5 : list.size();
                    }
                    processItems = new ArrayList<ProcessItem>();
                    for (int i = 0; i < list.size(); i++) {
                        ProcessItem sortItem = new ProcessItem();
                        sortItem.percent = MemoryUtils.getProcessAvailableRam(CleanApplication.getInstance(),
                                list.get(i)) * 100 / mTitleRam;
                        sortItem.icon = list.get(i).icon;
                        sortItem.label = list.get(i).label;
                        sortItem.pkgName = list.get(i).pkgName;
                        processItems.add(sortItem);
                    }
                }
            });
        }
    }

    private ArrayList<ProcessItem> getUnProcessItems() {
        ArrayList<com.tools.cleanmaster.taskmain.ProcessItem> list =
                TKPubApi.getRunningProcesses(CleanApplication.getInstance(), true);
        int mTitleRam = MemoryUtils.getAvailableRam(CleanApplication.getInstance(), list);
        if (mTitleRam == 0) {
            mTitleRam = list.size() < 1 ? 5 : list.size();
        }
        ArrayList<ProcessItem> process = new ArrayList<ProcessItem>();
        for (int i = 0; i < list.size(); i++) {
            ProcessItem sortItem = new ProcessItem();
            sortItem.percent = MemoryUtils.getProcessAvailableRam(CleanApplication.getInstance(), list.get(i)) * 100 / mTitleRam;
            sortItem.icon = list.get(i).icon;
            sortItem.label = list.get(i).label;
            sortItem.pkgName = list.get(i).pkgName;
            process.add(sortItem);
        }
        return process;
    }

    public void onDestroyProcess() {
        if (mHandler != null) {
            mHandler.removeCallbacksAndMessages(null);
        }
    }
}
